package org.example.service.Impl;

import lombok.AllArgsConstructor;
import org.example.dao.Dynamic;
import org.example.dao.User;
import org.example.enums.responseEnum;
import org.example.mapper.DynamicMapper;
import org.example.service.DynamicService;
import org.example.service.UserService;
import org.example.util.MinioUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@AllArgsConstructor
@Service
public class DynamicServiceImpl implements DynamicService {

    private final DynamicMapper dynamicMapper;

    private final MinioUtil minioUtil;

    private final UserService userService;

    @Override
    public Integer addDynamic(Dynamic dynamic) {
        if (dynamic.getUid() == null || dynamic.getText() == null) {
            return -2;
        }
        Integer i = dynamicMapper.addDynamic(dynamic);
        System.out.println("service打印" + dynamic.toString());
        if (i > 0) {
            return dynamic.getId();
        } else {
            return -1;
        }
    }

    @Override
    public responseEnum updateDynamic(Dynamic dynamic) {
        return null;
    }

    @Override
    public responseEnum addDynamicImage(Dynamic dynamic) {
        //根据id获取动态数据
        Dynamic dynamic1 = dynamicMapper.getDynamic(dynamic);
        String image = dynamic1.getImage();
        System.out.println("获取image数据"+image);
        //判断图片是否为空
        if (image==null) {
            //为空则直接添加进去
            dynamicMapper.updateDynamic(dynamic);
        } else {
            //不为空则将新图片拼接在后面，然后修改
            dynamic1.setImage(dynamic1.getImage() + ";" + dynamic.getImage());
            dynamicMapper.updateDynamic(dynamic1);
        }
        return responseEnum.success;
    }

    //代码后期请求动态数据采用分页getDynamicListByPage，本函数为前置版本
    @Override
    public List<Dynamic> getDynamicList() {
        //获取全部动态数据
        List<Dynamic> dynamicList = dynamicMapper.getDynamicList();
        //为每一条动态添加images
        minioUtil.getDynamicUrl(dynamicList);
        //为每一条动态添加用户信息
        for (Dynamic dynamic : dynamicList) {
            User user=new User();
            user.setId(dynamic.getUid());
            User userById = userService.getUserById(user);
            dynamic.setUser(userById);
        }
        return dynamicList;
    }

    @Override
    public List<Dynamic> getDynamicListByPage(Integer page, Integer size) {
        if (page==null||size==null){
            return null;
        }
        List<Dynamic> dynamicList = dynamicMapper.getDynamicListByPage((page - 1) * size, size);
        //为每一条动态添加images
        minioUtil.getDynamicUrl(dynamicList);
        //为每一条动态添加用户信息
        for (Dynamic dynamic : dynamicList) {
            User user=new User();
            user.setId(dynamic.getUid());
            User userById = userService.getUserById(user);
            dynamic.setUser(userById);
        }
        return dynamicList;
    }

    @Override
    public int getMaxPageNumber(int pageSize) {
        int totalRecords = dynamicMapper.getTotalRecords();
        return (int) Math.ceil((double) totalRecords / pageSize);
    }


}
